package faircat.android.exphone;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.lang.reflect.Method;

public class BluetoothSocketFactory {
	public static final int TYPE_RFCOMM = 1;
	public static final int TYPE_SCO = 2;
	public static final int TYPE_L2CAP = 3;
	
	public static final BluetoothSocket createBluetoothSocket(int type,
			int fd, boolean auth, boolean encrypt, String address,
			int port) throws IOException{
		BluetoothSocket socket = null;
		try{
			Constructor<BluetoothSocket> cs = BluetoothSocket.class
					.getDeclaredConstructor(int.class, int.class,
							boolean.class, boolean.class, String.class,
							int.class);
			if( !cs.isAccessible() ){
				cs.setAccessible(true);
			}
			socket = cs.newInstance(type, fd, auth, encrypt, address, port);			
		}
		catch(SecurityException e){
			Log.e("BluetoothSocketFactory", "constructory", e);			
		}
		catch(NoSuchMethodException e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		catch(IllegalArgumentException e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		catch(InstantiationException e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		catch(IllegalAccessException e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		catch(InvocationTargetException e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		catch(Exception e){
			Log.e("BluetoothSocketFactory", "constructory", e);	
		}
		return socket;
	}
}
